package com.origon360.utilities;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.origon.entities.Product;
import com.origon.entities.Shop;
import com.origon.entities.User;
import com.origon.managedbeans.BeanStore;
import com.origon.managedbeans.LoginBean;

public class SessionUtil {
	
	public static final String LOGIN_BEAN_NAME = "loginBean";
	public static final String BEAN_STORE_NAME ="beanStore";
	
	public static FacesContext getFacesContext(){
		return FacesContext.getCurrentInstance();
	
	}
	
	public static ExternalContext getExternalContext(){
		return getFacesContext().getExternalContext();
	}
	
	public static HttpServletRequest getHttpServletRequest(){
			HttpServletRequest request = (HttpServletRequest) getExternalContext().getRequest();
		return request;
	}

	public static HttpServletResponse getHttpServletResponse(){
			HttpServletResponse response = (HttpServletResponse) getExternalContext().getResponse();
		return response;
	}
	
	public static <T> T getManagedBean(final String beanName, Class<T>clazz){
			FacesContext context = getFacesContext();
		return (T) context.getApplication().evaluateExpressionGet(context, "#{" +beanName+"}", clazz);
	}
	
	public static User getSessionUser(){
			User loginUser = getManagedBean(LOGIN_BEAN_NAME, LoginBean.class).getUser();
		return loginUser;
	}
	
	public static void setSessionUser(User user){
		getManagedBean(LOGIN_BEAN_NAME, LoginBean.class).setUser(user);
	}
	
	
	public static Shop getSelectedShop(){
			Shop selectedShop = getManagedBean(BEAN_STORE_NAME, BeanStore.class).getSelectedShop();
		return selectedShop;
	}
	
	public static void setSelectedShop(Shop selectedShop)
	{
		getManagedBean(BEAN_STORE_NAME, BeanStore.class).setSelectedShop(selectedShop);
	}
	
	
	public static Product getSelectedProduct(){
			Product selectedProduct = getManagedBean(BEAN_STORE_NAME, BeanStore.class).getSelectedProduct();
		return selectedProduct;
	}
	
	public static void setSelectedProduct(Product selectedProduct){
		getManagedBean(BEAN_STORE_NAME, BeanStore.class).setSelectedProduct(selectedProduct);
	}
	
	public static String getParam(Object key){
		return getExternalContext().getRequestParameterMap().get(key);
	}
	
	public static Flash flashScope(){
		return getExternalContext().getFlash();
	}
	
	
	
}
